﻿using Sys.Common;
using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Core.Sys
{
    /// <summary>
    /// 用户菜单按钮业务
    /// </summary>
    public class UserMenuButtonBll : ExampleBase<SysUserMenuButton, UserMenuButtonBll>
    {
        /// <summary>
        /// 根据用户获取实体
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns>符合条件的结果集合</returns>
        public IQueryable<SysUserMenuButton> EntitiesByUserID(string id)
        {
            return Entities.Where(m => m.UserID.Equals(id));
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <param name="data">分配数据</param>
        /// <returns>操作结果</returns>
        public bool SetPurview(string id, string data)
        {
            string[] mblist = data.Split(',');

            var list = new List<SysUserMenuButton>();

            foreach (string mb in mblist)
            {
                if (!string.IsNullOrEmpty(mb))
                {
                    var item = new SysUserMenuButton();

                    item.ID = StringHelper.GetGuid();
                    item.UserID = id;
                    item.MenuButtonID = mb;

                    list.Add(item);
                }
            }

            Delete(m => m.UserID.Equals(id));

            return Insert(list) == list.Count;
        }
    }
}
